Back to course home
0% completed
Vote For New Content
Amazon Hard Problems
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Minimum Suffix Flips (medium)
Problem Statement
You are given a target binary string of length n. Initially, you also have another string str of length n containing all 0s. You need to make str equal to the target by performing some number of operations.
In one operation, you can take an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Here, Flip means changing '1' to '0' and '0' to '1'.
Return the minimum number of operations required to make string str equal to target.
Examples
-
Example 1:
- Input: target =
"001" - Expected Output: 1
- Justification: Flipping the last character in the
strstring from0to1will make the string"001", achieving the goal in one operation.
- Input: target =
-
Example 2:
- Input: target =
"11001" - Expected Output: 3
- Justification: Starting with
sas"00000", the process to makesequal to"11001"is as follows:- Flip the last three digits to get
"00111". - Flip all five digits to get
"11000". - Flip the last digit to get
"11001". This process requires three operations, thus the expected output is 3.
- Flip the last three digits to get
- Input: target =
-
Example 3:
- Input: target =
"100010" - Expected Output: 4
- Justification: Beginning with
sas"000000", to transformsinto"100010", the sequence of operations could be:- Flip the last two digits to get
"000011". - Flip the last digit to get
"000010". - Flip all six digits to get
"111101". - Flip the last five digits to get
"100010". This sequence involves four operations, making the expected output 4.
- Flip the last two digits to get
- Input: target =
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Count the Number of Good Subarrays
Solution: Minimum Suffix Flips
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself